#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>


#define CALLBACK


typedef unsigned long       DWORD;
typedef int                 BOOL;
typedef unsigned char       BYTE;
typedef unsigned short      WORD;
typedef unsigned long		ULONG;
typedef unsigned short		USHORT;
typedef unsigned char		UCHAR;
typedef short				SHORT;

#include "SDS_Device_Api.h"


int CALLBACK OnEventCB(long long llUserData, const char* strEvent, int event_type)
{
    printf("on event %s ---\r\n", strEvent);
    printf("---\r\n");
    return 0;
}



int CALLBACK OnFrameCB(SDS_Device::sdk::_handle stream, BYTE* lpBuf, int size, long long llStamp, int type, int channel, long long llUserData)
{
    static int add = 0;
    if (type == sds::device::em_FrameType_Video_IFrame
            || type == sds::device::em_FrameType_Video_VFrame)
    {
        printf("Video frame received, channel: %d, size: %d\n", channel, size);
    }
    else if (type == sds::device::em_FrameType_Audio_FrameT)   // G.711
    {
        printf("G.711 frame\n");
    }
    else if (type == sds::device::em_FrameType_Audio_Frame)   // G.726
    {
        printf("G.726 frame\n");
    }
    else
    {
        printf("other frame\n");
    }

    if(add++ > 100)
    {
        printf("recv frame  type %d   size  %d    time  %lld    channel  %d ---\r\n", type, size, llStamp, channel);
        add = 0;
    }

    return 0;
}

int main( int argc, char** argv )
{

    printf("Test application start!  \r\n");

    SDS_Device::sdk::st_work_param param;
//	int k = SDS_NetDevice_test();
    memset(&param, 0, sizeof(param));
    strcpy(param.server, "192.168.1.102");
    param.port = 7706;
    param.callbackEvent = OnEventCB;
    param.callbackFrame = OnFrameCB;
    param.llUserData = (long long)0;
    SDS_Device::sdk::SDS_NetDevice_Init(param);

    SDS_Device::sdk::_handle stream = NULL;

    printf("server start!  \r\n");
    printf("enter number: \r\n");
    printf("1  - open device's channel 1, main rate stream\r\n");
    printf("2  - open device's channel 1, sub rate stream\r\n");
    printf("3  - open device's channel 1, audio stream\r\n");
    printf("4  - close stream\r\n");
    printf("5  - ptz up\r\n");
    printf("6  - ptz down\r\n");
    printf("7  - exit\r\n");

    char szTemp[256] = { 0 };
    while(true)
    {
        fgets(szTemp, sizeof(szTemp) - 1, stdin);
        printf("gets  %s   \r\n", szTemp);

        //delete   return  key
        szTemp[strlen(szTemp)-1] = '\0';
        int len = strlen(szTemp);

        printf("gets2  %s  %d \r\n", szTemp, len);

        //do command
        if (strncmp(szTemp, "7", 1) == 0)
        {
            printf("gets3  %s  %d \r\n", szTemp, len);

            printf("break!  \r\n");
            break;
        }
        else if(strncmp(szTemp, "1", 1) == 0)
        {
            printf("gets3  %s  %d \r\n", szTemp, len);
            //SDS_Device::sdk::SDS_NetDevice_OpenStream("10007", 1, sds::device::eStreamType::eMainStream, stream);
            SDS_Device::sdk::SDS_NetDevice_OpenStream("10007", 1, 0, stream);
            continue;
        }
        else if(strncmp(szTemp, "2", 1) == 0)
        {
            printf("gets3  %s  %d \r\n", szTemp, len);
            //SDS_Device::sdk::SDS_NetDevice_OpenStream("10007", 1, sds::device::eStreamType::eMainStream, stream);
            SDS_Device::sdk::SDS_NetDevice_OpenStream("10007", 1, 1, stream);
            continue;
        }
        else if(strncmp(szTemp, "3", 1) == 0)
        {
            printf("gets3  %s  %d \r\n", szTemp, len);
            //SDS_Device::sdk::SDS_NetDevice_OpenStream("10007", 1, sds::device::eStreamType::eMainStream, stream);
            SDS_Device::sdk::SDS_NetDevice_OpenStream("10007", 1, 2, stream);
            continue;
        }
        else if(strncmp(szTemp, "4", 1) == 0)
        {
            printf("gets3  %s  %d \r\n", szTemp, len);
            SDS_Device::sdk::SDS_NetDevice_CloseStream(stream);
            continue;
        }
        else if(strncmp(szTemp, "5", 1) == 0)
        {
            printf("gets3  %s  %d \r\n", szTemp, len);
            SDS_Device::sdk::SDS_NetDevice_PtzCtrlOnStream(stream, sds::device::ePtzActionType::ePtzActionUp, 10, 0);
            continue;
        }
        else if(strncmp(szTemp, "6", 1) == 0)
        {
            printf("gets3  %s  %d \r\n", szTemp, len);
            SDS_Device::sdk::SDS_NetDevice_PtzCtrlOnStream(stream, sds::device::ePtzActionType::ePtzActionDown, 10, 0);
            continue;
        }
    }

    SDS_Device::sdk::SDS_NetDevice_Uninit();

    printf("server stop!  \r\n");

    return 0;
}
